﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    ///<summary>
    ///Represents a MenuSection control in the SharePoint Server Ribbon.
    ///</summary>
    public class RibbonMenuSection : RibbonControl, IRibbonContainer<RibbonControl>
    {
        #region Properties

        /// <summary>
        /// Gets or sets an enumeration value that determines how child controls in this section are rendered.
        /// </summary>
        public RibbonMenuDisplayMode DisplayMode = RibbonMenuDisplayMode.Menu;

        ///<summary>
        /// TRUE to make this section of the menu scrollable.
        ///</summary>
        public bool Scrollable
        {
            get { return _scrollable; }
            set { _scrollable = value; }
        }

        ///<summary>
        /// Specifies the maximum height of the section in pixels.
        ///</summary>
        public int MaxHeight { get; set; }

        /// <summary>
        /// Child controls of the section
        /// </summary>
        public IEnumerable<RibbonControl> Controls { get; set; }

        #endregion

        #region Internal

        internal override string XmlTag
        {
            get { return Schema.MenuSection; }
        }

        internal void SetMenuFullId(RibbonVisualControl menu)
        {
            foreach (var c in Controls)
            {
                if (c is RibbonMenuSection)
                {
                    ((RibbonMenuSection)c).SetMenuFullId(menu);
                }
                else if(c is RibbonFlyoutAnchor)
                {
                    ((RibbonFlyoutAnchor)c).SetMenuFullId(menu);
                }
                else if (c is RibbonButton)
                {
                    ((RibbonButton)c).ParentMenu = menu;
                }
            }
        }

        internal IEnumerable<RibbonControl> AllControls
        {
            get
            {
                var res = new List<RibbonControl>();

                foreach (var control in Controls.Where(s => s.Visible))
                {
                    var gallery = control as RibbonGallery;
                    if (gallery != null)
                    {
                        gallery.NameSpace = FullId;
                        res.AddRange(gallery.AllControls());
                    }
                    else
                    {
                        control.NameSpace = FullId + ".Controls";
                        res.Add(control);
                        var flyoutAnchor = control as RibbonFlyoutAnchor;
                        if (flyoutAnchor != null)
                        {
                            res.AddRange(flyoutAnchor.AllControls());
                        }
                    }
                }
                return res;
            }
        }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Add(new XAttribute(Schema.Title, Title));
            element.Add(new XAttribute(Schema.DisplayMode, DisplayMode.ToString()));
            if (MaxHeight > 0)
                element.Add(new XAttribute(Schema.MaxHeight, MaxHeight));
            element.Add(new XAttribute(Schema.Scrollable, Scrollable ? "true" : "false"));
            if (Controls.Any(c => c is RibbonGallery))
            {
                var gallery = Controls.OfType<RibbonGallery>().FirstOrDefault();
                if (gallery != null)
                {
                    gallery.NameSpace = FullId;
                    element.Add(gallery.AsXElement());
                }
            }
            else
            {
                var container = new XElement(Schema.Controls, new XAttribute(Schema.Id, FullId + ".Controls"));
                var controlIndex = 0;
                foreach (var control in Controls)
                {
                    controlIndex++;
                    control.TemplateAlias = string.Empty;
                    control.NameSpace = FullId + ".Controls";
                    control.Sequence = controlIndex;
                    container.Add(control.AsXElement());
                }
                element.Add(container);
            }
            return element;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonMenuSection.
        /// </summary>
        public RibbonMenuSection() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonMenuSection.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonMenuSection(string id, string title) : base(id, title) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonMenuSection.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonMenuSection(string id) : base(id) { }

        #endregion

        #region Private

        private bool _scrollable = true;

        #endregion
    }
}
